<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CronJOb extends CI_Controller {
    function __construct(){
		parent::__construct();
		//$this->load->model('games');
		$this->load->model('game'); 
	}
	
	// deifrerence in minutes
    function dateDiference($past,$present,$diference){
		$to_time = strtotime($present);
		$from_time = strtotime($past);
		if( round(abs($to_time - $from_time) / 60,2)<$diference){
			return true;
		}
		//echo 'false';
	}
	
    public function index(){
        $leagues = array();
	$leagues = $this->game->getLeagueNames();
		$this->load->library('prosoccer_parser');
		$parser= new Prosoccer_parser();
		//$arrayOfGames = $parser->parseMainPage('http://www.prosoccer.gr');	
		//http://www.prosoccer.gr/soccer-prediction-Tuce.htm
		//http://www.prosoccer.gr/soccer-prediction-Wece.htm
		//http://www.prosoccer.gr/soccer-prediction-Thce.htm
		//http://www.prosoccer.gr/soccer-prediction-Frce.htm
		//http://www.prosoccer.gr/soccer-prediction-Sace.htm
		//http://www.prosoccer.gr/soccer-prediction-Suce.htm
		//http://www.prosoccer.gr/soccer-prediction-Moce.htm	
		$parseTimeStart=time();
		$arrayOfGames = $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Tuce.htm');		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Wece.htm'));		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Thce.htm'));		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Frce.htm'));
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Sace.htm'));		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Suce.htm'));		
		$arrayOfGames = array_merge($arrayOfGames, $parser->parseMainPage('http://www.prosoccer.gr/soccer-prediction-Moce.htm'));
		$parseTime=time()-$parseTimeStart;
		
        // ucitaj sve utakmice iz baze koje nisu zavrsene (rezultat " - ") a u bazi su manje od 10 dana
        $dbGames = $this->game->getAllGamesFiltered();
		$contain = false;
		$insert=0;
		$update=0;
		$loopTimeStart=time();
        echo "<p>Number of items: ".sizeOf($arrayOfGames)."</p>";
        echo "<p>". time() ."</p>";
		foreach ($arrayOfGames as $gameCron){
            //Posto ti u crawl delu pokupis skracenice za lige i pravis GameTO sa skracenicom 
            //umesto sa IDem, ja ovde moram sad da resolvujem IDLeague preko skracenice.
            //Onda setujem Id umesto skracenice. Da bi bilo brze, sve lige(IDeve) i njihove skracenice
            //imam u nizu $leagues koji napunim na pocetku, da ne bih za svaki game posebno cimao bazu
            //-----------------------------------------------------------------------
            $IDLeague = 0;
            if(array_key_exists($gameCron->getLeague(), $leagues)){
                $IDLeague = $leagues[$gameCron->getLeague()];
            }else{
                $this->game->saveNewLeagueShortName($gameCron->getLeague());
                echo "<p>No league with specified ShortName: " . $gameCron->getLeague() . "</p>";
            }
            $gameCron->setLeague($IDLeague);
            //-----------------------------------------------------------------------
            //Ne razumem cemu sluzi contain i zasto izlazis iz petlje kad je true. 
            //-----------------------------------------------------------------------
			$contain =false;
			foreach ($dbGames as $gameDb){	
                // ako postoji u zadnjih 10 dana i home team i away team i  league i rezultat " - " onda --> update kvote 1, x, 2 , rezultat
                if ($this->dateDiference($gameDb->getTimestamp(),$gameCron->getTimestamp(),14400) && $gameDb->getHomeTeam()==$gameCron->getHomeTeam() && $gameDb->getAwayTeam()==$gameCron->getAwayTeam() && $gameDb->getLeague()==$gameCron->getLeague()){
                    if ((($gameDb->getResult()==' - ') && $gameCron->getResult()!=' - ') || ($gameDb->getPercentFix1()=='') || ($gameDb->getPercentFixX()=='') || ($gameDb->getPercentFix2())==''){
						$this->game->updateGame($gameDb,$gameCron);
						$update++;
					}
					$contain =true;
					break;
				}			
			}
			if (!($contain)){
                echo "<p>".$gameCron->toString()."</p>";
				$this->game->insertGame($gameCron);
				$insert++;
				$contain =false;
			}
		}
        echo "<p>". time() ."</p>";
//	    $loopTime=time()-$loopTimeStart;
//	    $data = array(
//		'parseTime' => $parseTime,
//		'loopTime' => $loopTime,
//		'insert' => $insert,
//		'update' => $update,				
//	    );	
            //$this->load->view('cronJob',$data);
	}
}
?>

